//
//  DoctorChatViewController.m
//  QuickCure1
//
//  Created by summer的Dad on 16/11/1.
//  Copyright © 2016年 maoge. All rights reserved.
//
#import "DoctorApplyListModel.h"
#import "DoctorChatViewController.h"
#import "DoctorChatView.h"
#import "DoctorChatTableViewCell.h"
#import "DoctorOtherTableViewCell.h"
#import "MessageModel.h"
static NSString* CELLID_OTHER = @"cellid";
static NSString* CELLID_ME = @"cellidb";
@interface DoctorChatViewController ()
//@property(nonatomic,strong)UITableView* listTableView;

@property(nonatomic,weak)UIView* chatView;

@property(nonatomic,weak)UITableView* tableViews;

@end

@implementation DoctorChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addData];
       [self.tableViews scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.chatModels.count -1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
//    NSLog(@"%zd",self.chatModels.count);
    //监听键盘弹出的事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popupKeyboardNotification :) name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark
#pragma mark - 加载数据
- (void)addData
{
    //读取数据
    self.chatModels = [[[MessageModel alloc] init]ZQ_arrayWithPlistName:@"messages.plist" className:@"MessageModel"];
   //判断时间
//    NSLog(@"1111111%@",self.chatModels);
   NSString *tempTime = nil;
    
    for (MessageModel *model in _chatModels)
    {
        if ([model.time isEqualToString:tempTime])
        {
            model.time = @"";
        }
        else
        {
            tempTime = model.time;
        }
    }
    
    [self.tableView reloadData];
   
}

#pragma mark
#pragma mark - UI界面
-(void)setupUI{
self.title = self.model.doctor_name;
    //创建tableView
    [self addTableView];
    
    //创建底部View
}
//建立tableView
- (void)addTableView
{
    //    //建立tableView
    UITableView* tableView = [[UITableView alloc] initWithFrame:SCREEN_BOUNDS style:UITableViewStylePlain];
//    self.tableView = tableView;
    
    
    tableView.dataSource = self;
    tableView.delegate = self;
    self.tableViews = tableView;
//    self.listTableView = tableView;
    [self.view addSubview:tableView];
    //设置分割线类型
    tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    
    //注册
    [tableView registerClass:[DoctorChatTableViewCell class] forCellReuseIdentifier:CELLID_OTHER];
    [tableView registerClass:[DoctorOtherTableViewCell class] forCellReuseIdentifier:CELLID_ME];
    
    
    //估算行高
//        self.tableView.estimatedRowHeight = 200;
//        self.tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.rowHeight = 120;
    //底部的聊天框
    DoctorChatView* chatView = [[DoctorChatView alloc] init];
    
    
    
    [self.view addSubview:chatView];
    
    [chatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.left.equalTo(self.view);
        make.top.equalTo(tableView.mas_bottom);

    }];
    _chatView = chatView;
    
    //约束tableView
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-40);

    }];
//    UIView* bottonView = [[UIView alloc] init];
//    
//    bottonView.backgroundColor = [UIColor orangeColor];
//    [self.view addSubview:bottonView];
//    
//    [bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view);
//        make.leading.trailing.equalTo(self.view);
//        make.height.equalTo()
//        
//    }];
//    
    
  

}

#pragma mark
#pragma mark - 监听键盘弹起
//监听键盘弹起的的方法
- (void)popupKeyboardNotification :(NSNotification *)notification
{
    CGRect endFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat endY = endFrame.origin.y;
    CGFloat transfromY = [UIScreen mainScreen].bounds.size.height - endY;
    
    self.view.transform = CGAffineTransformMakeTranslation(0, -transfromY);
    
}
#pragma mark
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.chatModels.count;
    
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MessageModel *model = _chatModels[indexPath.row];
    
    if (model.type == MessageTypeOther)
    {
        DoctorChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID_OTHER forIndexPath:indexPath];
        
        cell.model = model;
        
        return cell;
        
//        NSLog(@"%zd",self.chatModels);
    }
    
    DoctorOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID_ME forIndexPath:indexPath];
    
        cell.model = model;
    
    return cell;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
